<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Abdullah Zainul Abidin, Nuhaa All Bakry
 *          Eavay Javay Barnad, Sarogini Muniyandi, Teow Jit Huan
 *
 */

class TitlesController extends AppController {

    /**
     * Define $name
     *
     */
    var $name = 'Titles';
    /**
     * Define $helpers
     *
     */
    var $helpers = array('Html', 'Form');

    /**
     * Describe index
     *
     * @return null
     */
    function index() {
        //$this->layout='mainpage';
        $this->Title->recursive = 0;
        // first page
        if (empty($this->params['named']['page']) || empty($this->data)) {
            $this->Session->delete('Search.short'); 
            $this->Session->delete('Search.long'); 
        }

        // get the search terms
        if(!empty($this->data['Search']['short'])) $cursearch_short = $this->data['Search']['short'];
        elseif($this->Session->check('Search.short')) $cursearch_short = $this->Session->read('Search.short'); 
        else $cursearch_short = '';
        if(!empty($this->data['Search']['long'])) $cursearch_long = $this->data['Search']['long'];
        elseif($this->Session->check('Search.long')) $cursearch_long = $this->Session->read('Search.long'); 
        else $cursearch_long = '';

        // construct queries
        $filters = array();
        if(isset($cursearch_short) && $cursearch_short!='') {
            array_push($filters,"Title.short_name  like '%".$cursearch_short."%'");
            $this->Session->write('Search.short_name', $cursearch_short);        
        }
        if(isset($cursearch_long) && $cursearch_long!='') {
            array_push($filters,"Title.long_name  like '%".$cursearch_long."%'");
            $this->Session->write('Search.long_name', $cursearch_long);              
        }
        $this->set('titles', $this->paginate('Title',$filters));
    }

    /**
     * Describe view
     *
     * @param $id
     * @return null
     */
    function view($id = null) {
        //$this->layout='mainpage';
        if (!$id) {
            $this->Session->setFlash(__('Invalid Title.', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->set('dtitle', $this->Title->read(null, $id));
    }

    /**
     * Describe add
     *
     * @return null
     */
    function add() {
        //$this->layout='mainpage';
        if (!empty($this->data)) {
            $this->Title->create();
            if ($this->Title->save($this->data)) {
                $this->Session->setFlash(__('The Title has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The Title could not be saved. Please, try again.', true));
            }
        }
    }

    /**
     * Describe edit
     *
     * @param $id
     * @return null
     */
    function edit($id = null) {
        //$this->layout='mainpage';
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid Title', true));
            $this->redirect(array('action'=>'index'));
        }
        if (!empty($this->data)) {
            if ($this->Title->save($this->data)) {
                $this->Session->setFlash(__('The Title has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The Title could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Title->read(null, $id);
        }
    }

    /**
     * Describe delete
     *
     * @param $id
     * @return null
     */
    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for Title', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->Title->delete($id);
        $this->Session->setFlash(__('Title deleted', true));
        $this->redirect(array('action'=>'index'));
    }

}
?>
